package servlet;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;

@WebServlet(name = "enterRoomServlet", urlPatterns = "*.enter", loadOnStartup = 1)
public class EnterRoom extends HttpServlet {

    private static class EntranceData {
        String roomId;
        String hostCamp;
        String size;
    }

    @Override
    public void init() throws ServletException {
        super.init();
    }

    private static ConcurrentHashMap<String, EntranceData> entranceDataMap = new ConcurrentHashMap<>();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html; charset=utf-8");
        String temp;
        //http://localhost:8080/Z5COnline/go.enter?roomId=abcd&hostCamp=black&size=15
        switch(req.getServletPath()) {
            case "/go.enter": {
                if((temp = req.getParameter("roomId")) != null) {
                    EntranceData entranceData = new EntranceData();
                    entranceData.roomId   = temp;
                    entranceData.hostCamp = req.getParameter("hostCamp");
                    if(entranceData.hostCamp == null) entranceData.hostCamp = "black";
                    entranceData.size     = req.getParameter("size");
                    if(entranceData.size == null) entranceData.size = "15";
                    //req.setAttribute("entranceData", entranceData);
                    entranceDataMap.put(req.getSession().getId(), entranceData);
                    //req.getRequestDispatcher("/battleground.html").forward(req, resp);
                    resp.sendRedirect(req.getContextPath() + "/battleground.html");
                }
                break;
            }
            case "/get.enter":
                //Object data = req.getAttribute("entranceData");
                EntranceData entranceData = entranceDataMap.remove(req.getSession().getId());
                JSONObject respData = new JSONObject();

                if(entranceData != null) {
                    //EntranceData entranceData = (EntranceData)data;
                    respData.put("type",     "getRoomId");
                    respData.put("roomId",   entranceData.roomId);
                    respData.put("hostCamp", entranceData.hostCamp);
                    respData.put("size",     entranceData.size);
                } else {
                    respData.put("type",     "roomIdNotFound");
                }
                resp.getWriter().print(respData.toJSONString());
                break;
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
